package com.platform.modules.chat.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.chat.dao.ChatGroupMonitorDao;
import com.platform.modules.chat.dao.ChatUserMonitorDao;
import com.platform.modules.chat.domain.ChatGroupMonitor;
import com.platform.modules.chat.domain.ChatUserMonitor;
import com.platform.modules.chat.enums.RiskTypeEnum;
import com.platform.modules.chat.service.ChatGroupMonitorService;
import com.platform.modules.chat.service.ChatGroupService;
import com.platform.modules.chat.service.ChatUserMonitorService;
import com.platform.modules.wallet.service.WalletRedPacketService;
import com.platform.modules.wallet.service.WalletTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 好友申请表 服务层实现
 * q3z3
 * </p>
 */
@Service("chatUserMonitorService")
public class ChatUserMonitorServiceImpl extends BaseServiceImpl<ChatUserMonitor> implements ChatUserMonitorService {

    @Resource
    private ChatUserMonitorDao chatUserMonitorDao;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(chatUserMonitorDao);
    }

    @Resource
    private WalletRedPacketService redPacketService;
    @Resource
    private WalletTransferService transferService;
    @Resource
    private ChatGroupMonitorService groupMonitorService;


    private ChatUserMonitor create(Long groupId, Long userId) {
        ChatUserMonitor groupMonitor = new ChatUserMonitor();
        groupMonitor.setUserId(userId);
        groupMonitor.setGroupId(groupId);
        groupMonitor.setProfit(0.00f);
        groupMonitor.setUp(0.00f);
        groupMonitor.setDown(0.00f);
        groupMonitor.setInTotal(0);
        groupMonitor.setOutTotal(0);
        groupMonitor.setUpdateTime(DateUtil.date());
        groupMonitor.setRisk(RiskTypeEnum.NORMAL);
        add(groupMonitor);
        return groupMonitor;
    }

    @Override
    public ChatUserMonitor getByUserId(Long groupId, Long userId) {

        ChatUserMonitor userMonitor = queryOne(new ChatUserMonitor()
                .setUserId(userId)
                .setGroupId(groupId));

        if (ObjectUtil.isEmpty(userMonitor)) {
            userMonitor = create(groupId, userId);
        }
        return userMonitor;
    }

    @Override
    public ChatUserMonitor getTotal(Long groupId, Long userId) {

        ChatUserMonitor userMonitor = getByUserId(groupId, userId);

        Integer redpacketTotal = redPacketService.queryTodayByGroupIdAndUserId(groupId, userId);
        userMonitor.setOutTotal(redpacketTotal);

        ChatGroupMonitor groupMonitor = groupMonitorService.getByGroupId(groupId);
        if (ObjectUtil.isNotEmpty(groupMonitor) && ObjectUtil.isNotEmpty(groupMonitor.getFinance())) {
            Integer transfer = transferService.queryUserToday(userId, groupMonitor.getFinance());
            userMonitor.setInTotal(transfer);
        } else {
            userMonitor.setInTotal(0);
        }

        if (userMonitor.getInTotal().intValue() > 0) {
            BigDecimal profit = new BigDecimal(userMonitor.getInTotal() - userMonitor.getOutTotal()).divide(new BigDecimal(userMonitor.getOutTotal()), 3, BigDecimal.ROUND_DOWN);
            userMonitor.setProfit(profit.floatValue());
        } else {
            userMonitor.setProfit(0.000f);
        }


        if (userMonitor.getProfit().floatValue() == 1.000f ||
                userMonitor.getUp().floatValue() == 0.000f ||
                userMonitor.getDown().floatValue() == 0.000f) {
            userMonitor.setRisk(RiskTypeEnum.NORMAL);
        } else {

            if (userMonitor.getProfit() < userMonitor.getDown()) {
                userMonitor.setRisk(RiskTypeEnum.YES);
            } else if (userMonitor.getProfit() > userMonitor.getUp()) {
                userMonitor.setRisk(RiskTypeEnum.NO);
            } else {
                userMonitor.setRisk(RiskTypeEnum.NORMAL);
            }
        }

        userMonitor.setUpdateTime(DateUtil.date());
        updateById(userMonitor);

        return userMonitor;
    }

}
